<?php
declare (strict_types=1);

namespace addons\substation;

use think\Addons;
use think\facade\Db;
use think\facade\Event;
use think\facade\Route;

class Substation extends Addons
{
    public function install()
    {
        $sql = $this->addon_path.'data'.DIRECTORY_SEPARATOR.'install.sql';
        create_sql($sql);
		return true;
    }

    public function uninstall()
    {
        return true;
    }

    // 插件升级时的处理(可选)
    public function upgrade()
    {
        $config = Db::name('app')->where(['name'=>'substation'])->value('config');
        if (!empty($config)) {
            $arr = json_decode($config,true);
            if (!empty($arr)) {
                $this->addonConfigSaveHook($arr);
            }
        }

        // 对旧版本检查是否有lang字段
        $prefix = Db::getConfig('connections.mysql.prefix');

        $substation = Db::query("show COLUMNS FROM {$prefix}substation");
        $sql = "ALTER TABLE {$prefix}substation ADD COLUMN `lang` VARCHAR(20) NOT NULL DEFAULT '' COMMENT '语言标识'";;
        foreach ($substation as $key=>$value) {
            if ($value['Field']=='lang') {
                $sql = '';
                break;
            }
        }
        if ($sql) {
            Db::execute($sql);
        }

        return true;
    }

    public function enable()
    {
        // 菜单数组文件路径
        $menu = include $this->addon_path.'data'.DIRECTORY_SEPARATOR.'menu.php';
        // 参数一给路径，参数二给当前插件的标识
        create_menu($menu,$this->getName());
        // 伪静态
        $url_rewrite = Db::name('config')->where(['name'=>'url_rewrite'])->find();
        $array = json_decode($url_rewrite['value'], true);
        if (!in_array('substation/index', $array)) {
            $array['substation/index'] = '/substation$.html';
        }
        if (!in_array('substation/index', $array)) {
            $array['substation/list'] = '/substation/:math/$';
        }
        $json = json_encode($array);
        Db::name('config')->where(['name'=>'url_rewrite'])->update(['value'=>$json]);

        \app\admin\model\routine\Config::initConfig(true);
        return true;
    }

    public function disable()
    {
        del_menu($this->getName());

        // 清理伪静态
        $url_rewrite = Db::name('config')->where(['name'=>'url_rewrite'])->find();
        $array = json_decode($url_rewrite['value'], true);
        unset($array['substation/index']);
        unset($array['substation/list']);
        Db::name('config')->where(['name'=>'url_rewrite'])->update(['value'=>json_encode($array)]);
        \app\admin\model\routine\Config::initConfig(true);
        return true;
    }

    /**
     * 插件初始化
     */
    public function addonsInitHook()
    {
        // 加载扩展语言包
        app()->lang->load($this->addon_path.'data'.DIRECTORY_SEPARATOR.$this->app->lang->getLangset().'.php');

        // 加载标签
        $view = $this->app->config->get('view');
        $view['taglib_pre_load'] = $view['taglib_pre_load'].',addons\substation\library\HkCms';
        $this->app->config->set($view,'view');

        Route::pattern([
            'tag' => '[a-zA-Z0-9\-_\x{4e00}-\x{9fa5}]+',
        ]);
    }

    /**
     * 配置保存事件
     * @param $param
     */
    public function addonConfigSaveHook($param)
    {
        if (!isset($param['substation_model']['value'])) {
            return false;
        }
    }

    /**
     * 路由
     * @param $param
     */
    public function indexRouteHook($param)
    {
        Route::pattern([
            'tag' => '[\w\-]+',
        ]);
    }
}